package cn.wj.school.app.dto;

import cn.wj.school.common.Constants;
import cn.wj.school.common.dto.BaseMsg;
import cn.wj.school.common.enums.MsgType;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

/**
 * @Author: yueding
 * @Description: 登录响应体
 * @Date: Created in 17:44 2018/12/27
 * @Modified By: yueding Copyright(c) cai-inc.com
 */
@Data
public class LoginRespMsg extends BaseMsg {

    /**
     * 登录是否成功
     */
    private byte success = Constants.TRUE;

    private String error;

    /**
     * 用户信息,json串
     */
    private String userInfo;

    /**
     * 服务端ip
     */
    private String serverIp;

    @Override
    public void writeBody(ByteBuf buf) {
        buf.writeByte(success);
        if (success == Constants.FALSE) {
            writeUTF8(buf, error);
        } else {
            writeUTF8(buf, userInfo);
        }
        if (StringUtils.isNotBlank(serverIp)) {
            writeUTF8(buf, serverIp);
        }
    }

    @Override
    public void readBody(ByteBuf buf) {
        this.success = buf.readByte();
        if (this.success == Constants.FALSE) {
            this.error = readUTF8(buf);
        } else {
            this.userInfo = readUTF8(buf);
        }
        if (buf.isReadable()) {
            this.serverIp = readUTF8(buf);
        }
    }

    /**
     * 获取消息类型
     *
     * @return
     */
    @Override
    public MsgType getMsgType() {
        return MsgType.LOGIN;
    }
}
